package com.jiepos.mpos.biz.appInfM.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.biz.affix.service.AppAffixInfService;
import com.jiepos.mpos.common.constant.DBConstant;
import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.OrganizationDao;
import com.jiepos.mpos.common.entity.App_Affix_InfEntity;
import com.jiepos.mpos.common.entity.App_Ins_AppEntity;
import com.jiepos.mpos.common.entity.App_Ins_InfEntity;
import com.jiepos.mpos.common.entity.GpsEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.CollectionUtil;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.RSACoder;
import com.jiepos.mpos.core.util.StringUtils;

/**
 *
 *
 * <p>标题: —— 组织信息维护类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos]
 * @Package com.jiepos.mpos.biz.appInfM.service
 * @author 余翼膑
 * @version 1.0
 * @since 2017年9月22号
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class OrganizationService extends BaseService {

    @Autowired
    private OrganizationDao organizationDao;
    @Autowired
    private AppAffixInfService appAffixInfService;

    /**
     * 查询通道信息列表json分页数据
     * @param pagination
     * @param infEntity
     * @return
     */
    public Map getOrganizationServiceList(HttpServletRequest request, Pagination pagination, App_Ins_InfEntity infEntity){
        StringBuffer sb = getInitSql("t2").append(",");
        sb.append("t2.ins_app_uuid as uuid,t1.ins_cd,t2.SUPER_CUSTOM_ST,t1.zip_cd,t1.ins_cn_nm,t2.android_appid,t2.ios_appid,t2.ios_version,t2.android_version,t1.ins_type, t2.custom_auth_nopass_msg,t2.sms_password,t2.app_name_app,t2.jg_appkey,t2.jg_mastersecret, ");
        sb.append("t1.ins_type||'-'||t3.code_desc as instype,t2.adv_wait_tm,T2.APP_ADV_BATCHNO,t2.share_title,t2.share_desc ");
        sb.append(" from ");
        sb.append(DBConstant.Table.T_APP_INS_APP);
        sb.append(" t2 LEFT JOIN ");
        sb.append(DBConstant.Table.T_APP_INS_INF).append(" t1 ON t1.ins_uuid = t2.ins_uuid");
        sb.append(" LEFT JOIN ");
        sb.append(DBConstant.Table.T_APP_CODE_LIST).append( " t3 ").append(" ON t1.ins_type = t3.code_val and t3.code_tp='INS_TYPE' ");
        sb.append(" where 1=1 ");
        List list = new ArrayList();
        if (!StringUtils.isBlank(infEntity.getIns_cd())) {
            sb.append( " and t1.ins_cd like ? " );
            list.add("%" + infEntity.getIns_cd() + "%");
        }
        if (!StringUtils.isBlank(infEntity.getIns_cn_nm())) {
            sb.append( " and t1.ins_cn_nm like ? " );
            list.add("%" + infEntity.getIns_cn_nm() + "%");
        }
        pagination.queryString = sb.toString();
        pagination = getInsAuthSql(request, pagination, "t1", null);
        pagination = this.getOrderSql(pagination, "t1");
        return organizationDao.getPaginationMap(pagination,"t2",list);
    }

    /**
     * 根据记录id查询实体对象
     * @param   ins_app_uuid
     * @return
     */
    public Map getInsInfById(String ins_app_uuid) {
        if (StringUtils.isBlank(ins_app_uuid) || ins_app_uuid.equals("undefined")) {
            return new HashMap();
        }
        StringBuilder sb = new StringBuilder();
        sb.append("select T1.ins_app_uuid,t1.ins_uuid,t1.ios_appid,t1.ios_version,t1.ios_download_url,t1.ios_update_url,t1.android_appid,t1.android_version,t1.app_name_app,");
        sb.append("t1.android_download_url,t1.android_update_url,t1.msg_sign,t1.custom_auth_pass_st,t1.custom_auth_nopass_st,t1.custom_auth_pass_msg,t1.custom_auth_nopass_msg,t1.sms_username,t1.sms_password,t1.sms_type,t1.rec_id,t1.super_custom_st, ");
        sb.append(" t1.ins_uuid as uuid, t2.ins_cd||'-'||t2.ins_cn_nm  as inst_name,t2.ins_type||'-'||t3.code_desc as instype,t1.adv_wait_tm,t1.jg_appkey,t1.jg_mastersecret,T1.APP_ADV_BATCHNO,t1.share_title,t1.share_desc  ");
        sb.append( " from ");
        sb.append(DBConstant.Table.T_APP_INS_APP).append(" t1 LEFT JOIN ");
        sb.append(DBConstant.Table.T_APP_INS_INF).append(" t2 ").append(" LEFT JOIN ");
        sb.append(DBConstant.Table.T_APP_CODE_LIST);
        sb.append(" t3 ON t2.ins_type = t3.code_val and t3.code_tp='INS_TYPE'");
        sb.append("on t1.ins_uuid = t2.ins_uuid");
        sb.append(" WHERE T1.ins_app_uuid = ?");
        List<String> list = new ArrayList<>();
        list.add(ins_app_uuid);
        List<Map<String, Object>> results = organizationDao.queryForList(getOrderSql(sb.toString(), "t1"), list);
        if(!CollectionUtil.isEmpty(results)){
            Map<String, Object> map = results.get(0);
            String sms_password = String.valueOf(map.get("SMS_PASSWORD"));
            // 短信通道密码 解密
            byte[] rsaPrivateKeys = new byte[0];
            try {
                rsaPrivateKeys = RSACoder.decryptByPrivateKey(RSACoder.decryptBASE64(sms_password),SystemConstant.PRI_KEY_STR);
            } catch (Exception e) {
                e.printStackTrace();
            }
            sms_password=  RSACoder.encryptBASE64(rsaPrivateKeys);
           /* sms_password = new String(rsaPrivateKeys);*/
            map.put("SMS_PASSWORD" ,sms_password);
            return  map;
        }else{
            return  new HashMap<String, Object>();
        }
    }
    public String exitInsApp(App_Ins_AppEntity appInsAppEntity){
        //校验机构是否存在
        Boolean getInsAppUuid = this.getInsUuid(appInsAppEntity.getIns_app_uuid(),appInsAppEntity.getIns_uuid());
        if(!getInsAppUuid){
            return SystemConstant.FAIL_INSID_3;
        }
        //校验安卓或者ios的appid是否唯一
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT t1.ins_app_uuid FROM t_app_ins_app t1  ")
                .append(" where t1.ios_appid = ? or t1.android_appid = ?  ");
        List list = new ArrayList();
        list.add(appInsAppEntity.getIos_appid());
        list.add(appInsAppEntity.getAndroid_appid());
        List<Map<String, Object>> results = organizationDao.queryForList(getOrderSql(sql.toString(), "t1"), list);
        Map dataMap = new HashMap();
        if (CollectionUtil.isEmpty(results)) {
            return SystemConstant.SUCCESS_APPID_2;
        } else if (results != null && results.size() > 0) {
                dataMap = results.get(0);
                if (appInsAppEntity.getIns_app_uuid().equals(dataMap.get("ins_app_uuid"))) {
                    return SystemConstant.SUCCESS_APPID_2;
                }
                return SystemConstant.FAIL_APPID_1;
            }
        return SystemConstant.FAIL_APPID_1;
    }

    /**
     * 保存机构
     * @param  appInsAppEntity
     * @param map
     */
    public void saveOrganization(App_Ins_AppEntity appInsAppEntity, Map map) {

        //当前用户
        appInsAppEntity.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
        //短信通道密码
        byte[] rsaPublicKeys = new byte[0];
        try {
            //公钥加密
            rsaPublicKeys = RSACoder.encryptByPublicKey(RSACoder.decryptBASE64(appInsAppEntity.getSms_password()), SystemConstant.PUB_KEY_STR);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String s = RSACoder.encryptBASE64(rsaPublicKeys);
        appInsAppEntity.setSms_password(s);
        if(org.apache.commons.lang.StringUtils.isBlank(appInsAppEntity.getIns_uuid())){
            appInsAppEntity.setIns_uuid(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_INS_UUID)));
        }
        if (!StringUtils.isBlank(appInsAppEntity.getIns_app_uuid())) {
          //  App_Ins_AppEntity newInsAppEntity=organizationDao.findByParam("ins_app_uuid", appInsAppEntity.getIns_app_uuid());
          //  appInsAppEntity.setRec_id(appInsAppEntity.getRec_id());
            appInsAppEntity = (App_Ins_AppEntity) this.saveCommonData(appInsAppEntity ,map,"update" );
            List noChangeList = new ArrayList();
            noChangeList.add("ins_app_uuid");
            noChangeList.add("rec_crt_ts");
            organizationDao.update(appInsAppEntity, noChangeList);
        } else {
            appInsAppEntity.setLast_oper_in(SystemConstant.OPER_ADD);
            appInsAppEntity.setIns_app_uuid(IDFactoryUtil.getUUID());
            App_Ins_AppEntity newInsAppEntity = (App_Ins_AppEntity) this.saveCommonData(appInsAppEntity, map, "null");
            organizationDao.save(appInsAppEntity);
        }
    }
    private Boolean getAppId(String ios_appid,String android_appid ){
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" SELECT t1.ios_appid,t1.android_appid FROM t_app_ins_app t1  ")
                .append(" where t1.ios_appid = ? or t1.android_appid = ?  ");
        List list = new ArrayList();
        list.add(ios_appid);
        list.add(android_appid);
        List<Map<String, Object>> results = organizationDao.queryForList(getOrderSql(sqlBuf.toString(), "t1"), list);
        Map dataMap = new HashMap();
        if (CollectionUtil.isEmpty(results)) {
            return true;
        } else {
            if (results != null && results.size() > 0) {
                dataMap = results.get(0);
                if (ios_appid.equals(dataMap.get("ios_appid")) & android_appid.equals(dataMap.get("android_appid"))) {
                    return true;
                }
                return false;
            }
        }
        return false;
    }

    /**
     * 校验是否app是否存在
     * @param ins_app_uuid
     * @return
     */
    private Boolean getInsUuid(String ins_app_uuid,String ins_uuid) {
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" SELECT t1.ins_app_uuid,t1.ins_uuid FROM t_app_ins_app t1  ")
                .append(" where t1.ins_app_uuid = ? or t1.ins_uuid = ? ");
        List list = new ArrayList();
        list.add(ins_app_uuid);
        list.add(ins_uuid);
        List<Map<String, Object>> results = organizationDao.queryForList(getOrderSql(sqlBuf.toString(), "t1"), list);
        Map dataMap = new HashMap();
        if (CollectionUtil.isEmpty(results)) {
            return true;
        } else {
            if (results != null && results.size() > 0) {
                dataMap = results.get(0);
                dataMap.get("ins_app_uuid");
                if (ins_app_uuid.equals(dataMap.get("ins_app_uuid"))) {
                    return true;
                }
                return false;
            }
        }
        return false;
    }
    /**
     * 所属机构树形
     * @param
     * @return
     */
    public List<Map<String, Object>> getAppInfTree(String searchText){
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" SELECT T1.INS_UUID ID, ")
                .append(" T1.SUP_INS_UUID PID, ")
                .append(" T1.INS_CD || '-' || T1.INS_CN_NM NAME, ")
                .append("  T.app_name_app as APPNAME, ")
                .append("  T.INS_APP_UUID as INSUUID ")
                .append(" FROM T_APP_INS_APP T  ")
                .append(" INNER JOIN T_APP_INS_INF T1 ON  T.INS_UUID = T1.INS_UUID ")
                .append(" where 1 = 1");
        List<String> param = new ArrayList<>();
        if(!StringUtils.isBlank(searchText)) {
            sqlBuf.append(" and (INS_CD like ? or INS_CN_NM like ? ) ");
            param.add("%" + searchText + "%");
            param.add("%" + searchText + "%");
        }
        List<Map<String, Object>> ins = organizationDao.queryForList(getOrderSql(sqlBuf.toString(), "T"), param);
        return ins;
    }
    
    /**
     * 获取机构app信息
     * @param gpsEntity
     * @return
     */
    public Map getInsAppMap(GpsEntity gpsEntity){
    	String appType = gpsEntity.getAppType();
    	StringBuilder sb = new StringBuilder();
    	sb.append("select t1.ins_app_uuid as \"ins_app_uuid\",t1.ios_version as \"ios_version\",");
    	sb.append("t1.ios_download_url as \"ios_download_url\",t1.ios_update_url as \"ios_update_url\",");
    	sb.append("t1.android_version as \"android_version\",t1.android_download_url as \"android_download_url\",");
    	sb.append("t1.android_update_url as \"android_update_url\",t1.adv_wait_tm as \"adv_wait_tm\", ");
    	sb.append("t1.super_custom_st as \"super_custom_st\", ");
    	sb.append("t1.app_adv_batchno as \"app_adv_batchno\",t1.ins_uuid as \"ins_uuid\" ");
    	sb.append(" from "+DBConstant.Table.T_APP_INS_APP+" t1 ");
    	sb.append(" where t1.rec_st="+SystemConstant.REC_ST_1 );
    //	String bakAppType = "2";//2表示安卓
    	if(SystemConstant.APP_TYPE_IOS.equals(appType)){
    		sb.append(" and t1.ios_appid=?");    
    	//	bakAppType = "1";
    	}else{
    		sb.append(" and t1.android_appid=?");
    	}
    	List paramList = new ArrayList();
    	paramList.add(gpsEntity.getApp_id());
    	List<Map> list = organizationDao.queryForList(sb.toString(),paramList);	
    	
    	Map insAppMap = new HashMap();
    	if(list != null && list.size() > 0){
    		insAppMap = list.get(0);
    		String ins_app_uuid = String.valueOf(insAppMap.get("ins_app_uuid"));
    		String ins_uuid = String.valueOf(insAppMap.get("ins_uuid"));
    		String insSql = "select phone from "+DBConstant.Table.T_APP_INS_INF+" where ins_uuid='"+ins_uuid+"'";
    		List<Map> insList = organizationDao.queryForList(insSql);
    		if(insList != null && insList.size() > 0){
    			Map insMap = insList.get(0);
    			String super_custom_login_id = String.valueOf(insMap.get("phone"));
    			insAppMap.put("super_custom_login_id", super_custom_login_id);
    		}
    		
    		StringBuilder insAppSql = new StringBuilder();
    		insAppSql.append("select t1.ins_app_history_uuid,t1.app_desc,t1.android_update_md5 ,");
    		insAppSql.append("t1.android_version_code,t1.android_apk_size  from "+DBConstant.Table.T_APP_INS_APP_HISTORY+" t1");
    		insAppSql.append(" where t1.ins_app_uuid=? ");
    		insAppSql.append(" and t1.app_type=? and t1.rec_st="+SystemConstant.REC_ST_1  );
    		insAppSql.append(" order by t1.rec_id desc");
    		List insParamList = new ArrayList();
    		insParamList.add(ins_app_uuid);
    		insParamList.add(appType);
    		List<Map> insAppList = organizationDao.queryForList(insAppSql.toString(),insParamList);
    		if(insAppList != null && insAppList.size() > 0){
    			Map insAppHisMap = insAppList.get(0);
            	if(SystemConstant.APP_TYPE_IOS.equals(appType)){
            		insAppMap.put("ios_update_desc", StringUtils.objToString(insAppHisMap.get("app_desc")));
            		insAppMap.put("android_update_desc", "");
            	}else{
            		insAppMap.put("ios_update_desc","" );
            		insAppMap.put("android_update_desc", StringUtils.objToString(insAppHisMap.get("app_desc")));
            		insAppMap.put("android_update_md5", StringUtils.objToString(insAppHisMap.get("android_update_md5")));
            		insAppMap.put("android_version_code", StringUtils.objToString(insAppHisMap.get("android_version_code")));
            		insAppMap.put("android_apk_size", StringUtils.objToString(insAppHisMap.get("android_apk_size")));
            		//获取apk上传路径
                    App_Affix_InfEntity entity = appAffixInfService.getAffixInfByParam(StringUtils.objToString(insAppHisMap.get("ins_app_history_uuid")),
                                       SystemConstant.AFFIX_TP_05,SystemConstant.STORE_TP_05_01);
                    if(entity != null){
                        String update_url = StringUtils.objToString(insAppMap.get("android_update_url")) + StringUtils.objToString(entity.getAffix_path());
                        insAppMap.put("android_update_url",update_url);
                    }
            	}
    		}    
    		
    	}
    	return insAppMap;
    }


    /**
     * 根据appid加载 机构app设置信息
     * @param apptype
     * @param appid
     * @return
     */
    public Map<String,Object> getInsAppInfoByAppid(String apptype,String appid){
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" SELECT I.INS_UUID,I.INS_CD,A.IOS_APPID,A.ANDROID_APPID,A.IOS_VERSION,A.ANDROID_VERSION,A.MSG_SIGN,A.SMS_TYPE,A.SMS_USERNAME,A.SMS_PASSWORD FROM ( ")
                .append("   SELECT INS_UUID, ")
                .append("        IOS_APPID, ")
                .append("        ANDROID_APPID, ")
                .append("        IOS_VERSION, ")
                .append("        ANDROID_VERSION,")
                .append("        MSG_SIGN, ")
                .append("        SMS_TYPE, ")
                .append("        SMS_USERNAME, ")
                .append("        SMS_PASSWORD ")
                .append("   FROM  "+ DBConstant.Table.T_APP_INS_APP)
                .append("   WHERE (IOS_APPID = ? OR ANDROID_APPID = ?) AND REC_ST != '0' ")
                .append(" ) A ")
                .append("   LEFT JOIN "+DBConstant.Table.T_APP_INS_INF+" I ")
                .append("   ON A.INS_UUID = I.INS_UUID ");

        List<String> paramList = new ArrayList<>();
        paramList.add(appid);
        paramList.add(appid);
        List<Map<String,Object>> list = organizationDao.queryForList(sqlBuf.toString(), paramList);
        if(list!=null && list.size() > 0){
            return list.get(0);
        }else{
            return new HashMap<>();
        }
    }


    /**
     * 根据ins_uuid加载 机构app设置信息
     * @param ins_uuid
     * @return
     */
    public Map<String,Object> getInsAppInfoByInsUUID(String ins_uuid){
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" SELECT I.INS_UUID,I.INS_CD,A.IOS_APPID,A.ANDROID_APPID,A.MSG_SIGN,A.SMS_TYPE,A.SMS_USERNAME,A.SMS_PASSWORD FROM ( ")
                .append("   SELECT INS_UUID, ")
                .append("        IOS_APPID, ")
                .append("        ANDROID_APPID, ")
                .append("        MSG_SIGN, ")
                .append("        SMS_TYPE, ")
                .append("        SMS_USERNAME, ")
                .append("        SMS_PASSWORD ")
                .append("   FROM  "+ DBConstant.Table.T_APP_INS_APP)
                .append("   WHERE INS_UUID = ? AND REC_ST != '0' ")
                .append(" ) A ")
                .append("   LEFT JOIN "+DBConstant.Table.T_APP_INS_INF+" I ")
                .append("   ON A.INS_UUID = I.INS_UUID ");

        List<String> paramList = new ArrayList<>();
        paramList.add(ins_uuid);
        List<Map<String,Object>> list = organizationDao.queryForList(sqlBuf.toString(), paramList);
        if(list!=null && list.size() > 0){
            return list.get(0);
        }else{
            return new HashMap<>();
        }
    }


    /**
     * 检查版本更新
     * @param gpsEntity
     * @return
     */
    @Deprecated
    public Map checkVersion(GpsEntity gpsEntity) {

        String appType = gpsEntity.getAppType();
        StringBuilder sb = new StringBuilder();
        sb.append("select t1.ins_app_uuid as \"ins_app_uuid\", t1.ios_version as \"ios_version\",");
        sb.append("t1.ios_update_url as \"ios_update_url\",t1.ios_download_url as \"ios_download_url\",");
        sb.append("t1.android_version as \"android_version\",t1.android_update_url as \"android_update_url\",");
        sb.append("t1.android_download_url as \"android_download_url\", ");
        sb.append("t1.app_adv_batchno as \"app_adv_batchno\" ");
            sb.append(" from " + DBConstant.Table.T_APP_INS_APP + " t1 ");
        sb.append(" where t1.rec_st=" + SystemConstant.REC_ST_1);
       /* String bakAppType = "2";//2表示安卓*/
        if (SystemConstant.APP_TYPE_IOS.equals(appType)) {
            sb.append(" and t1.ios_appid=?");
           /* bakAppType = "1";*/
        } else {
            sb.append(" and t1.android_appid=?");
        }
        List paramList = new ArrayList();
        paramList.add(gpsEntity.getApp_id());
        List<Map> list = organizationDao.queryForList(sb.toString(), paramList);

        Map insAppMap = new HashMap();
        if (list != null && list.size() > 0) {
            insAppMap = list.get(0);
            String ins_app_uuid = String.valueOf(insAppMap.get("ins_app_uuid"));
            StringBuilder insAppSql = new StringBuilder();
            insAppSql.append("select t1.app_desc,t1.android_update_md5,t1.android_version_code,t1.android_apk_size  from " + DBConstant.Table.T_APP_INS_APP_HISTORY + " t1");
            insAppSql.append(" where t1.ins_app_uuid=? ");
            insAppSql.append(" and t1.app_type=? and t1.rec_st=" + SystemConstant.REC_ST_1);
            insAppSql.append(" order by rec_id desc");
            List insParamList = new ArrayList();
            insParamList.add(ins_app_uuid);
            insParamList.add(appType);
            List<Map> insAppList = organizationDao.queryForList(insAppSql.toString(), insParamList);
            if(insAppList != null && insAppList.size() > 0) {
                Map insAppHisMap = insAppList.get(0);
                if (SystemConstant.APP_TYPE_IOS.equals(appType)) {
                    insAppMap.put("ios_update_desc", StringUtils.objToString(insAppHisMap.get("app_desc")));
                    insAppMap.put("android_update_desc", "");
                } else {
                    insAppMap.put("ios_update_desc", "");
                    insAppMap.put("android_update_desc", StringUtils.objToString(insAppHisMap.get("app_desc")));
                    insAppMap.put("android_update_md5", StringUtils.objToString(insAppHisMap.get("android_update_md5")));
                    insAppMap.put("android_version_code", StringUtils.objToString(insAppHisMap.get("android_version_code")));
                    insAppMap.put("android_apk_size", StringUtils.objToString(insAppHisMap.get("android_apk_size")));
                }
            }
        }
        return insAppMap;
    }
    
    /**
     * 查询出所有正常状态的app
     * @return
     */
    public List<App_Ins_AppEntity> queryAllInsApps(){
    	StringBuffer sql = new StringBuffer();
		sql.append("SELECT INS_UUID FROM ")
		.append(Table.T_APP_INS_APP)
		.append(" WHERE REC_ST = '")
		.append(SystemConstant.NORMAL_STATUS).append("' ORDER BY REC_CRT_TS DESC");
		List<App_Ins_AppEntity> insApps = organizationDao.queryForListByParam(sql.toString());
		return insApps;
    }

    /**
     * 查询分享说明
     * @param custom_no
     * @return
     */
    public String getShareDesc(String custom_no) {
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" SELECT T.SHARE_DESC  ")
                .append(" FROM "+Table.T_APP_INS_APP+" T ")
                .append("  RIGHT JOIN "+Table.T_APP_CUSTOM_INF+" t1  ")
                .append(" on t.ins_uuid = t1.ins_uuid ")
                .append("  where T1.CUSTOM_NO = ? ");
        List list = new ArrayList();
        list.add(custom_no);
        List<Map<String, Object>> commisResult= organizationDao.queryForList(sqlBuf.toString(), list);
        if(StringUtils.isEmpty(commisResult)){
            Map map = new HashMap();
            map.put("share_desc","");
        }
        return StringUtils.objToString(commisResult.get(0).get("share_desc"));
    }
    /**
     * 查询分享标题
     * @param custom_no
     * @return
     */
    public String getShareTitle(String custom_no) {
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" SELECT T.SHARE_TITLE  ")
                .append(" FROM "+Table.T_APP_INS_APP+" T ")
                .append("  RIGHT JOIN "+Table.T_APP_CUSTOM_INF+" t1  ")
                .append(" on t.ins_uuid = t1.ins_uuid ")
                .append("  where T1.CUSTOM_NO = ? ");
        List list = new ArrayList();
        list.add(custom_no);
        List<Map<String, Object>> commisResult= organizationDao.queryForList(sqlBuf.toString(), list);
        if(StringUtils.isEmpty(commisResult)){
            Map map = new HashMap();
            map.put("share_desc","");
        }
        return StringUtils.objToString(commisResult.get(0).get("share_title"));
    }
    
    /**
     * 获取所有O单机构信息，供页面查询条件使用
     * @return
     */
    public List<Map<String, String>> getAllAppIns(HttpServletRequest request){
    	StringBuffer sql = new StringBuffer();
    	sql.append("SELECT II.INS_UUID, II.INS_CD || '-' || II.INS_CN_NM INS_NAME")
    	.append("  FROM ").append(Table.T_APP_INS_INF).append(" II, ").append(Table.T_APP_INS_APP).append(" IA")
    	.append(" WHERE II.INS_UUID = IA.INS_UUID")
    	.append("   AND II.REC_ST = IA.REC_ST")
    	.append("   AND II.").append(SystemConstant.REC_ST).append(" = '").append(SystemConstant.REC_ST_1).append("'");
    	return organizationDao.queryForList(getInsAuthSql(request, sql.toString(), "II", null), null);
    }

    /**
     * 获取机构唯一主键
     * @return
     */
    public Map getAppInsUuid(String ins_uuid){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT T.Android_Update_Url,T.INS_APP_UUID ")
                .append("  FROM ").append(Table.T_APP_INS_APP)
                .append(" T  where t.ins_uuid = ? and  t.rec_st = "+SystemConstant.REC_ST_1+ " order by rec_id desc");
        List list = new ArrayList();
        list.add(ins_uuid);
        List<Map> resultList = organizationDao.queryForList(sql.toString(), list);
        Map dateMap = new HashMap();

        if(CollectionUtil.isEmpty(resultList)){
            return null;
        }

        dateMap.put("INS_APP_UUID",String.valueOf(resultList.get(0).get("INS_APP_UUID")));
        dateMap.put("Android_Update_Url",String.valueOf(resultList.get(0).get("Android_Update_Url")));
        return dateMap;
    }
}
